﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Brjnk.GpxExplorer.Workspace.BitmapExport;

namespace Brjnk.GpxExplorer.UI
{
    /// <summary>
    /// Interaction logic for BitmapExportEditorWindow.xaml
    /// </summary>
    public partial class BitmapExportEditorWindow : Window
    {
        private BitmapExportEditor editor;

        public BitmapExportEditorWindow(BitmapExportEditor editor)
        {
            InitializeComponent();
            this.DataContext = editor;
            this.editor = editor;
            this.Closing += new System.ComponentModel.CancelEventHandler(BitmapExportEditorWindow_Closing);
        }

        private void BitmapExportEditorWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var retVal = UserMessages.Question(App.Instance.GetLocalizationString("$Loc$questionCloseExportBitmapEditor"),
                App.Instance.GetLocalizationString("$Loc$yes"), App.Instance.GetLocalizationString("$Loc$no"));
            if ((retVal ?? 1) == 1) e.Cancel = true;
            else if (optionsWindow != null && optionsWindow.IsVisible) optionsWindow.Close();
        }

        private BitmapExportOptionsWindow optionsWindow;

        //Options
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (optionsWindow != null && optionsWindow.IsVisible) optionsWindow.Focus();
            else
            {
                optionsWindow = new BitmapExportOptionsWindow(editor);
                optionsWindow.Show();
            }
        }

        //Cancel
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //OK
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            editor.CreateResult(outputSource);
            this.Close();
        }

        //PreviewButton
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            var window = new BitmapExportPreviewWindow();
            window.Image = editor.GetResult(outputSource);
            window.ShowDialog();
        }

        private void tabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Waypoints
            if (tabs.SelectedIndex == 0) { editor.SelectedLabel = lvWaypointLabels.SelectedItem as LabelBase; }
            //Routes
            else if (tabs.SelectedIndex == 1) { editor.SelectedLabel = lvRouteLabels.SelectedItem as LabelBase; }
            //Tracks
            else if (tabs.SelectedIndex == 2) { editor.SelectedLabel = lvTrackLabels.SelectedItem as LabelBase; }
            //User labels
            else if (tabs.SelectedIndex == 3) { editor.SelectedLabel = lvUserLabels.SelectedItem as LabelBase; }
            else throw new Exception("Invalid SelectedIndex.");
        }

        private void Select(LabelBase label)
        {
            if (label is UserLabel)
            {
                tabs.SelectedIndex = 3;
                lvUserLabels.SelectedItem = label;
            }
            else if (label is WaypointLabel)
            {
                tabs.SelectedIndex = 0;
                lvWaypointLabels.SelectedItem = label;
            }
            else if (label is RouteLabel)
            {
                tabs.SelectedIndex = 1;
                lvRouteLabels.SelectedItem = label;
            }
            else if (label is TrackLabel)
            {
                tabs.SelectedIndex = 2;
                lvTrackLabels.SelectedItem = label;
            }
        }

        private LabelBase movingLabel;
        private Point oldPoint;

        private void MouseDownHandler(Object sender, MouseButtonEventArgs e)
        {
            var lvi = (ContentPresenter)sender;
            var label = (LabelBase)lvi.DataContext;
            movingLabel = label;
            Select(label);
        }

        private void MouseUpHandler(Object sender, MouseButtonEventArgs e)
        {
            movingLabel = null;
        }

        private void MouseMoveHandler(Object sender, MouseEventArgs e)
        {
            var currentPoint = e.GetPosition((FrameworkElement)sender);
            if (e.LeftButton == MouseButtonState.Released) movingLabel = null;
            if (movingLabel != null)
            {
                var v = currentPoint - oldPoint;
                movingLabel.Move(v);
            }
            oldPoint = currentPoint;
        }

        //Vytvoření popisku z kontextového menu
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            editor.AddUserLabel(Mouse.GetPosition(outputSource));
        }
    }
}